#!/usr/bin/ruby
# -*- ruby -*-
#
# http://codeforces.com/problemset/problem/398/D

require 'pp'

n = m = q = o = nil

online = []
friends = []
commands = []

class Person
  attr_accessor :user_id, :friends, :friends_online, :status

  def initialize(user_id)
    self.user_id = user_id
    self.friends = {}
    self.friends_online = 0
    self.status = :offline
  end

  def online
    self.status = :online

    self.friends.each do |(friend_id, friend)|
      friend.friend_online(self.user_id)
    end
  end

  def offline
    self.status = :offline

    self.friends.each do |(friend_id, friend)|
      friend.friend_offline(self.user_id)
    end
  end

  def add_friend(other)
    self.friends[other.user_id] = other

    other.friend_online(self.user_id) if status == :online
  end

  def del_friend(other)
    self.friends.delete(other.user_id)

    other.friend_offline(self.user_id) if status == :online
  end

  def friend_online(friend_user_id)
    self.friends_online += 1
  end

  def friend_offline(friend_user_id)
    self.friends_online -= 1
  end
end

class Chat
  attr_accessor :users

  def initialize(n, online, friends)
    self.users = {}

    1.upto(n) do |user_id|
      self.users[user_id] = Person.new(user_id)
    end

    online.each do |online_user|
      self.users[online_user].online
    end

    friends.each do |(a, b)|
      add_friend(a, b)
    end
  end

  def online(user_id)
    self.users[user_id].online
  end

  def offline(user_id)
    self.users[user_id].offline
  end

  def add_friend(a, b)
    self.users[a].add_friend(self.users[b])
    self.users[b].add_friend(self.users[a])
  end

  def del_friend(a, b)
    self.users[a].del_friend(self.users[b])
    self.users[b].del_friend(self.users[a])
  end

  def count_online_friends(user_id)
    return self.users[user_id].friends_online
  end
end

ARGF.each_with_index do |line, index|
  next if line.strip.empty?

  if n.nil?
    n, m, q = line.strip.split(' ').map(&:to_i)
  elsif o.nil?
    o = line.to_i
  elsif o > 0
    o -= 1

    # Turn online
    online << line.to_i
  elsif m > 0
    m -= 1

    # Turn into friends
    friends << line.strip.split(' ').map(&:to_i)
  else
    commands << line.strip
  end
end

chat = Chat.new(n, online, friends)

commands.each do |command|
  tokens = command.split(' ')

  case tokens.first
  when 'O'
    chat.online(tokens.last.to_i)
  when 'F'
    chat.offline(tokens.last.to_i)
  when 'A'
    chat.add_friend(tokens[1].to_i, tokens[2].to_i)
  when 'D'
    chat.del_friend(tokens[1].to_i, tokens[2].to_i)
  when 'C'
    puts chat.count_online_friends(tokens.last.to_i)
  end
end
